//
//  AppDelegate.m
//  SeaMelonSeeds_Counselor
//
//  Created by 杨笑 on 2017/7/3.
//  Copyright © 2017年 杨笑. All rights reserved.
//

#import "AppDelegate.h"
#import <EaseUI/EaseUI.h>
#import <UserNotifications/UserNotifications.h>
#import "ChatDemoHelper.h"
#import "Tool.h"
#import "Config.h"
#import "AppDelegate+EaseMob.h"
#import <IQKeyboardManager.h>
#import "AppDelegate+WXApi.h"
#import "DBAccess.h"


@interface AppDelegate () <UNUserNotificationCenterDelegate,JPUSHRegisterDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [DBAccess sharedInstance];
    
    NSString *apnsCertName = nil;
#if DEBUG
    apnsCertName = @"apns_dev";
#else
    apnsCertName = @"apns_pro";
#endif
    
    if (NSClassFromString(@"UNUserNotificationCenter")) {
        [UNUserNotificationCenter currentNotificationCenter].delegate = self;
    }
    
    //注册微信
    [self registerWX];
    
    //注册环信
    [self easemobApplication:application didFinishLaunchingWithOptions:launchOptions appkey:@"1121170824115953#haiguazi" apnsCertName:apnsCertName otherConfig:@{kSDKConfigEnableConsoleLogger:[NSNumber numberWithBool:YES]}];
    
    [IQKeyboardManager sharedManager].toolbarDoneBarButtonItemText = @"完成";
    
    
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    RootTaBarController *rootVC = [RootTaBarController new];
    self.window.rootViewController = rootVC;
    self.rootTabBarController = rootVC;
    [self.window makeKeyAndVisible];
    
    [ChatDemoHelper shareHelper].mainVC = rootVC;
    NSString *isOpen = [Config userDefaultsGetObjectForKey:kIsOpenRemotePush];
    if (isOpen) {
        BOOL is = [isOpen boolValue];
        if (!is) {
            [[UIApplication sharedApplication] unregisterForRemoteNotifications];
        }
    }
    return YES;
}

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    if (_rootTabBarController) {
        [_rootTabBarController jumpToChatList];
    }
    [self easemobApplication:application didReceiveRemoteNotification:userInfo];
}

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
    if (_rootTabBarController) {
        [_rootTabBarController didReceiveLocalNotification:notification];
    }
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
    NSDictionary *userInfo = notification.request.content.userInfo;
    [self easemobApplication:[UIApplication sharedApplication] didReceiveRemoteNotification:userInfo];
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
{
    if (_rootTabBarController) {
        [_rootTabBarController didReceiveUserNotification:response.notification];
    }
    completionHandler();
}


-(void)applicationDidBecomeActive:(UIApplication *)application{
    NSLog(@"---applicationDidBecomeActive----");
    //进入前台
    [[NSNotificationCenter defaultCenter]postNotificationName:kAppDidBecomeActive object:nil];
}



@end
